Repository navigation
CUDA: CUDA-core FMA path for GTX 16 (Turing without tensor cores) - #134
PedroRossi wants to merge 4 commits into
Conversation
The GTX 16 series (TU116/TU117) is sm_75 but has no tensor cores. It still executes mma.sync m16n8k8, far slower than CUDA cores: on a GTX 1660 SUPER the f16 prefill ran about 3.5x slower than llama.cpp's. On sm_75, a "GTX 16" device name or STRATA_GEMM_FMA now compiles the existing CUDA-core branch of mma16816 (MMA_FMA), which covers the f16 prefill GEMM and the prefill attention. STRATA_GEMM_TC=1 keeps the tensor-core path. The startup line names the path that was compiled.
🔍 Aletheore evidence diffgraph LR
n0["src/cuda/mod.rs"]
n1["backend.rs"]
n2["src/hip/mod.rs"]
n3["device.rs"]
n4["lib.rs"]
n5["src/vulkan/mod.rs"]
n6["preflight.rs"]
n7["tests/common/mod.rs"]
n8["gpu_model.rs"]
n9["kv_q8.rs"]
n10["kv_turbo.rs"]
n11["llama_numerics.rs"]
n12["vulkan_split_kv.rs"]
n0 --> n1
n0 --> n2
n0 --> n3
n0 --> n4
n0 --> n5
n0 --> n6
n0 --> n7
n0 --> n8
n0 --> n9
n0 --> n10
n0 --> n11
n0 --> n12
classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
class n0 changed
class n1 dependent
class n2 dependent
class n3 dependent
class n4 dependent
class n5 dependent
class n6 dependent
class n7 dependent
class n8 dependent
class n9 dependent
class n10 dependent
class n11 dependent
class n12 dependent
What changed
Aggregate deltas
|
| fn turing_fma(name: &str, cc: (i32, i32), gemm_fma: bool) -> bool { | ||
| cc == (7, 5) && std::env::var_os("STRATA_GEMM_TC").is_none() && (gemm_fma || name.contains("GTX 16")) | ||
| } | ||
|
|
There was a problem hiding this comment.
🟠 High · Correctness · 1 of 5
Name-based detection misses other TU116/TU117 cards (MX450, T600 and similar). A laptop with an MX450 or a Quadro T600 (TU117, no tensor cores) reports a name without the substring "GTX 16", so turing_fma is false and it runs the slow m16n8k8 path that this change is meant to avoid. Gating on the device name is fragile, and compute capability alone cannot separate TU116/TU117 from TU102/104/106.
Prompt for your AI agent
File: src/cuda/mod.rs
Line: 381
Issue: Name-based detection misses other TU116/TU117 cards (MX450, T600 and similar). A laptop with an MX450 or a Quadro T600 (TU117, no tensor cores) reports a name without the substring "GTX 16", so turing_fma is false and it runs the slow m16n8k8 path that this change is meant to avoid. Gating on the device name is fragile, and compute capability alone cannot separate TU116/TU117 from TU102/104/106.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
There was a problem hiding this comment.
Fixed in 5e7755a. no_tensor_cores() now matches every TU116/TU117 product name: the GTX 16 series, MX450/MX550, T400-T2000 and CMP 30HX. The test covers those names, plus Turing cards that do have tensor cores (RTX 2070, Tesla T4, Quadro RTX 4000, TITAN RTX).
| let module = ctx.load_module(ptx(&source(kv_k_mode, kv_v_mode, !gemm_fma && (maj, min) < (7, 5)), arch, (maj, min))?).context("load cubin")?; | ||
| let src = source(kv_k_mode, kv_v_mode, !gemm_fma && (maj, min) < (7, 5)); | ||
| let src = if mma_fma { format!("#define MMA_FMA 1\n{src}") } else { src }; | ||
| let module = ctx.load_module(ptx(&src, arch, (maj, min))?).context("load cubin")?; |
There was a problem hiding this comment.
🟠 High · Correctness · 2 of 5
Module is built through ptx(&src, arch, cc); if that cache is keyed on arch and cc only, the MMA_FMA define is ignored. If ptx() memoises compiled output by (arch, cc) without the source text, a process that first compiles the default source on sm_75 and then loads the GTX 16 variant gets the tensor-core cubin. The FMA path is silently not used, and the device log still claims CUDA-core FMA. This cannot be verified from the diff.
Prompt for your AI agent
File: src/cuda/mod.rs
Line: 633
Symbol: new
Issue: Module is built through ptx(&src, arch, cc); if that cache is keyed on arch and cc only, the MMA_FMA define is ignored. If ptx() memoises compiled output by (arch, cc) without the source text, a process that first compiles the default source on sm_75 and then loads the GTX 16 variant gets the tensor-core cubin. The FMA path is silently not used, and the device log still claims CUDA-core FMA. This cannot be verified from the diff.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
There was a problem hiding this comment.
/dismiss This can't happen here. ptx() names the cached cubin cache_key(src, arch, &tag), and src is the full kernel source with #define MMA_FMA 1 prepended, so the two variants hash to different files. 5e7755a adds cubin_cache_key_changes_with_mma_fma to pin that.
| #[test] | ||
| fn turing_fma_only_for_gtx16_on_sm75() { | ||
| // Reads STRATA_GEMM_TC; the test does not set it, so skip when the caller did. | ||
| if std::env::var_os("STRATA_GEMM_TC").is_some() { return; } |
There was a problem hiding this comment.
✅ No longer detected as of 5e7755af89ea.
🔵 Low · Maintainability · 3 of 5
The new unit test returns early and passes vacuously when STRATA_GEMM_TC is set. A developer who has STRATA_GEMM_TC exported in their shell runs cargo test. turing_fma_only_for_gtx16_on_sm75 returns before any assertion and reports success, so a regression in the GTX 16 gating goes unnoticed. The test should set or remove the variable itself, or test a pure helper that takes the flag as a parameter.
Prompt for your AI agent
File: src/cuda/mod.rs
Line: 1849
Symbol: min_arch_error_allows_volta_and_newer
Issue: The new unit test returns early and passes vacuously when STRATA_GEMM_TC is set. A developer who has STRATA_GEMM_TC exported in their shell runs cargo test. turing_fma_only_for_gtx16_on_sm75 returns before any assertion and reports success, so a regression in the GTX 16 gating goes unnoticed. The test should set or remove the variable itself, or test a pure helper that takes the flag as a parameter.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
There was a problem hiding this comment.
Fixed in 5e7755a. turing_fma takes STRATA_GEMM_TC as a parameter (keep_tc), and the test passes both values explicitly, so the caller's environment no longer matters.
| __device__ __forceinline__ void mma16816(float* c, u32 a0, u32 a1, u32 a2, u32 a3, u32 b0, u32 b1) { | ||
| #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 800 | ||
| #if __CUDA_ARCH__ >= 750 | ||
| #if __CUDA_ARCH__ >= 750 && !MMA_FMA |
There was a problem hiding this comment.
🔵 Low · Maintainability · 4 of 5
The sm_75 MMA_FMA branch is only checked for NVRTC compilation, never for numerical equivalence with the tensor-core path. The FMA branch of mma16816 could compute the wrong product, such as a transposed B fragment or a wrong k-half offset. The new test only confirms that the source compiles for sm_75, so the wrong result would ship on GTX 16 cards unnoticed. A small GEMM comparing the FMA and m16n8k8 outputs would catch this.
Prompt for your AI agent
File: src/cuda/kernels.cu
Line: 1088
Issue: The sm_75 MMA_FMA branch is only checked for NVRTC compilation, never for numerical equivalence with the tensor-core path. The FMA branch of mma16816 could compute the wrong product, such as a transposed B fragment or a wrong k-half offset. The new test only confirms that the source compiles for sm_75, so the wrong result would ship on GTX 16 cards unnoticed. A small GEMM comparing the FMA and m16n8k8 outputs would catch this.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
There was a problem hiding this comment.
Checked on a GeForce GTX 1660 SUPER (driver 550.163.01, NVRTC 12.4) with 5e7755a, running the existing GPU tests that cover both users of mma16816:
| test | CUDA-core FMA (default on this card) | STRATA_GEMM_TC=1 (m16n8k8) |
|---|---|---|
kernels: gemm_matches_reference_for_every_type, small_batch_gemm_matches_tile_gemm |
pass | pass |
kernels: the four attn_prefill_* tests |
pass | pass |
gpu_model: gpu_forward_matches_cpu_reference, prefill_matches_reference (chunked prefill vs the CPU f32 reference), on Qwen3.6-35B-A3B-UD-Q2_K_XL.gguf |
pass | - |
greedy generate, 2,405-token prompt, 64 tokens |
same tokens as m16n8k8; prefill 211.8 tok/s |
prefill 100.6 tok/s |
So the FMA branch matches both the reference and the tensor-core path. No new test, since the existing ones exercise this product; they just need a GPU, which CI doesn't have.
| @@ -45,7 +47,7 @@ row. | |||
| | Arch | Matrix multiply | Int8 GEMM | Async copy | Shared memory per block | | |||
| | --- | --- | --- | --- | --- | | |||
| | sm_80 and newer | `mma.m16n8k16` | yes | `cp.async` | 100 KB or more | | |||
There was a problem hiding this comment.
🔵 Low · Maintainability · 5 of 5
The sm_75 row says nothing about STRATA_GEMM_FMA, but on sm_75 that variable now also switches the kernel branch. turing_fma returns true for any sm_75 card when gemm_fma is set, so an RTX 2070 user who sets STRATA_GEMM_FMA=1, which the row for sm_70 documents as Volta-only, now gets the CUDA-core path. Their performance changes and the documentation does not explain why.
Prompt for your AI agent
File: docs/SUPPORTED_GPUS.md
Line: 49
Issue: The sm_75 row says nothing about STRATA_GEMM_FMA, but on sm_75 that variable now also switches the kernel branch. turing_fma returns true for any sm_75 card when gemm_fma is set, so an RTX 2070 user who sets STRATA_GEMM_FMA=1, which the row for sm_70 documents as Volta-only, now gets the CUDA-core path. Their performance changes and the documentation does not explain why.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
There was a problem hiding this comment.
Fixed in 5e7755a. The sm_75 tier row now says that STRATA_GEMM_FMA=1 selects the CUDA-core branch on any sm_75 card, and that STRATA_GEMM_TC=1 keeps m16n8k8.
Aletheore Flash Lite reviewWhat this PR does. This pull request widens the Turing CUDA-core FMA fallback, which replaces the slower
1 finding(s) posted as inline review comment(s). 4 earlier finding(s) from previous pushes still open. 1 earlier finding(s) no longer detected in this push.
Review detailsReviewed 2 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 1 of 1 proposed finding(s). Fix all with your agent |
- Detect every Turing chip without tensor cores (TU116/TU117) by product name: the GTX 16 series, MX450/MX550, T400-T2000 and CMP 30HX, not only "GTX 16". - turing_fma takes STRATA_GEMM_TC as a parameter, so its test no longer depends on the caller's environment (it returned early when the variable was set). - New test: the cubin cache key changes with MMA_FMA, so a GTX 16 card can't load the tensor-core cubin. - Docs: STRATA_GEMM_FMA also selects the CUDA-core branch on sm_75.
| let src = super::source(0, 0, false); | ||
| let fma = format!("#define MMA_FMA 1\n{src}"); | ||
| assert_ne!(super::cache_key(&src, "sm_75", "12.4"), super::cache_key(&fma, "sm_75", "12.4")); | ||
| } |
There was a problem hiding this comment.
🔵 Low · Maintainability · 1 of 1
cubin_cache_key test passes for any prepended string, so it does not check the MMA_FMA define. The test prepends '#define MMA_FMA 1' to source(0, 0, false) and asserts the hash differs. Any prefix would change the hash, so the test still passes if the real GTX 16 source never gets the define, or if the define is applied to the wrong source. A regression in how the define is attached would go unnoticed.
Prompt for your AI agent
File: src/cuda/mod.rs
Line: 1880
Symbol: cache_key_depends_on_nvrtc_tag
Issue: cubin_cache_key test passes for any prepended string, so it does not check the MMA_FMA define. The test prepends '#define MMA_FMA 1' to source(0, 0, false) and asserts the hash differs. Any prefix would change the hash, so the test still passes if the real GTX 16 source never gets the define, or if the define is applied to the wrong source. A regression in how the define is attached would go unnoticed.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
…ustingStudio#135 (Messages API thinking blocks) Integration branch for the ai-lab stack image; not for upstream.
The fork's deploy/ai-lab branch now builds on upstream RustingStrata 1.4.2, with both patches as revised in review (RustingStudio/RustingStrata#134 and #135).
Summary
The GTX 16 series (TU116/TU117: GTX 1630, 1650, 1660 and their Ti/SUPER variants) reports compute capability 7.5 but
has no tensor cores. It still executes
mma.sync.aligned.m16n8k8, but far slower than the same work on its CUDA cores.On these cards, the f16 prefill GEMM and the prefill attention, the two users of
mma16816, run well below what thecard can do.
mma16816already has a CUDA-core branch: the sm_70 fallback behindSTRATA_GEMM_FMA. This PR lets sm_75 use it.kernels.cu: on sm_75,mma16816keeps its twom16n8k8steps unlessMMA_FMAis defined. With it, it takesthe existing CUDA-core branch.
mod.rs:turing_fma()turnsMMA_FMAon for sm_75 cards without tensor cores. CUDA doesn't report tensor cores,so
no_tensor_cores()goes by the TU116/TU117 product names: the GTX 16 series, MX450/MX550, T400-T2000, CMP 30HX.STRATA_GEMM_FMAturns it on for any sm_75 card, andSTRATA_GEMM_TC=1keeps the tensor-core path.sm_75 (Turing, CUDA-core FMA).turing_fma_only_for_turing_without_tensor_coreschecks the selection rules, by name and flags, and the startup line.cubin_cache_key_changes_with_mma_fmachecks that a GTX 16 card can't load the tensor-core cubin.nvrtc_compiles_full_source_for_sm75now also compiles the sm_75 source withMMA_FMA.SUPPORTED_GPUS.md(CUDA and Vulkan), and the sm_75 tier note.Decode doesn't go through
mma16816and is unchanged. Turing cards with tensor cores (RTX 20, Quadro RTX, T4) keepm16n8k8.Results
The test machine:
Qwen3.6-35B-A3B-UD-Q2_K_XL.gguf,--ctx 32768, MTP off. With 2,494 of 10,240 expertsresident, most expert work runs on the CPU.
rustingstrata bench --gpu --backend cuda --pp 4096,16384 --tg 128 --reps 2 --prompt-file <real text: project docs and C++ source>:m16n8k8(before; nowSTRATA_GEMM_TC=1)_dpkernels, no cooperative matrices)102.8 at 1200 MHz) with the GPU at 100% utilisation, which points to a compute-bound kernel.
calibrate(pp 512/4096 on the same text, 22 samples) kept the defaults: chunk 4096, MTP off. MTP 1/2/4 scored 0.91-0.98.rustingstrata check --backend vulkanpasses on this card.Tests
cargo test --releaseruns in a container with NVRTC 12.4 and no GPU, so the GPU and model tests skip themselves.turing_fma_only_for_turing_without_tensor_cores,cubin_cache_key_changes_with_mma_fma,nvrtc_compiles_full_source_for_sm75(with the newMMA_FMAcompile),device_log_names_turing_not_voltaanddevice_log_does_not_call_pascal_volta.#[ignore]speed tests), across 31 test binaries.The library's own tests show 164 passed, 1 ignored.
STRATA_GEMM_TC=1(m16n8k8)kernels:gemm_matches_reference_for_every_type,small_batch_gemm_matches_tile_gemmkernels: the fourattn_prefill_*testsgpu_model:gpu_forward_matches_cpu_reference,prefill_matches_reference(chunked prefill vs the CPU f32 reference)generate, 2,405-token prompt, 64 tokensm16n8k8; prefill 211.8 tok/s